<template>
    <div class="">

    </div>
</template>
<script>
    import userList from "@/util/userList"
    import {
        Storage
    } from '@/util/storage.js'
    import peerjs from "peerjs"
    var peer = ''
    export default {
        props: ['ischushi', 'srcObject'],
        data() {
            return {
                jcall: '', // 监听对方的呼叫 
            }
        },
        computed: {

        },
        components: {

        },
        methods: {
            /*拨号*/
            bohao(id, srcObject) {
                let call = peer.call(id, srcObject);
                call.on("stream", this.showVideo);
            },
            /*挂断*/
            guaduan(id) {
                console.log('挂断啊');
                let conn = peer.connect(id);
                conn.on('open', function() {
                    conn.on('data', function(data) {
                        console.log('Received', data);
                    });
                    conn.send('close');
                    peer.destroy()
                });
            },
            // 通话中直接断开
            HangUpTheMiddleCall() {
                peer.destroy()
                /*this.$router.back(-1)*/
            },
            /*接听*/
            answer() {
                this.$store.state.jcall.answer(this.srcObject);
                this.$store.state.jcall.on("stream", this.showVideo);
            },
            showVideo(se) {
                this.$emit('answer', se)
            },
            init() {
                let th = this
                var Storages = new Storage()
                if (Storages.getItem('userInfo')) {
                    peer = new peerjs(Storages.getItem('userInfo').id, {
                        url: "https://duxinggj.com:8089/video"
                    });
                    peer.on("open", (id) => {
                        console.log("我的 id 是：" + id);
                    })
                    peer.on("call", (call) => {
                        userList.map(a => {
                            if (a.id == call.peer) {
                                this.hf('callWaiting', a)
                            }
                        })
                        th.$store.state.jcall = call
                    });

                    peer.on('connection', function(e) {
                        e.on('data', function(data) {
                            if (data == 'close') {
                                th.hfp("/")
                                peer.destroy()
                            }
                        });
                    });
                    peer.on("close", function(e) {
                        console.log('close' + e);
                    });
                    peer.on("stream", function() {
                        console.log('stream111');
                    });

                    peer.on("destroy", function() {

                        console.log('destroy');
                    });
                    peer.on("disconnected", function() {
                        th.hfp("/")
                        peer.destroy()
                        console.log('disconnected');
                    });
                    peer.on("error", function(e) {
                        let str = '' + e
                        if (str.indexOf("Lost connection to server") >= 0) {
                            th.$toast.fail('连接服务失败');
                            return
                        }
                        if (str.indexOf("not connect") >= 0) {
                            th.$toast.fail('对方可能没在线');
                            peer.destroy()
                            th.hfp("/")
                            return
                        }

                        console.log("error" + e);
                    });
                }
            }
        },
        mounted() {
            if (this.ischushi) {
                this.init()
            }

        },
    }

</script>
<style scoped>


</style>
